package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * ClassName: OrderController
 * Package: com.atguigu.springcloud.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-28
 * Time: 22:22
 */

@RestController
@Slf4j
public class OrderController {

    //这个地址是写死 就算是8001配置了微服务也无法轮询访问
    //通过对外暴露的微服务名称来 访问
    //public static final String PAYMENT_URL = "http://localhost:8001";

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    //并且设置完 还没有结束 只是访问到集群了 但是 具体是集群里面的谁 还没有确定
    //还差 要开启一个负载均衡的功能

    //两个模块之间的调用 httpClient restTemplate 完成客户端 80 到服务端 8001 远程接口的调用

    //单独模块之间的 通信的类
    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        //一般读操作是get 写操作是post请求 模块之间访问的接口 参数 地址,传递参数,返回值的.class
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

}
